﻿// ASP.NET Web API Sample Application
// by Lee Dumond -- leedumond@gmail.com

namespace ContactManagerClient
{
   using System;
   using System.Collections.Generic;
   using System.Net.Http;
   using System.Net.Http.Headers;

   internal class Contact
   {
      public string Name { get; set; }
      public string Email { get; set; }
      public string Phone { get; set; }
   }

   internal class Program
   {
      private static void Main(string[] args)
      {
         var client = new HttpClient {BaseAddress = new Uri("http://localhost:19221/")};

         // Add a default Accept header.
         client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

         // Issue an HTTP GET request.
         HttpResponseMessage response = client.GetAsync("/api/contacts").Result;

         // do something with the response.
         if (response.IsSuccessStatusCode)
         {
            IEnumerable<Contact> contacts = response.Content.ReadAsAsync<IEnumerable<Contact>>().Result;

            foreach (Contact c in contacts)
            {
               Console.WriteLine("{0}\t{1}\t{2}", c.Name, c.Email, c.Phone);
            }
         }
         else
         {
            Console.WriteLine("{0} ({1})", (int) response.StatusCode, response.ReasonPhrase);
         }
      }
   }
}